package edu.ynu.se.xiecheng.achitectureclass.project.controller;

import edu.ynu.se.xiecheng.achitectureclass.project.dto.SoilDataDTO;
import edu.ynu.se.xiecheng.achitectureclass.project.dto.SubTableDataDTO;
import edu.ynu.se.xiecheng.achitectureclass.project.service.SensorDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/sensor")
public class SensorController {

    @Autowired
    private SensorDataService sensorDataService;

    /**
     * 触发获取传感器数据并存储到数据库
     *
     * @param boxNo 大棚编号
     * @param token 访问接口的 Token
     */
    @GetMapping("/fetch")
    public ResponseEntity<?> fetchSensorData(@RequestParam String boxNo, @RequestParam String token) {
        sensorDataService.fetchAndStoreSensorData(boxNo, token);
        return ResponseEntity.ok().body("Sensor data fetched and stored successfully.");
    }
    @GetMapping("/soil-data")
    public ResponseEntity<List<SoilDataDTO>> getAllSoilData() {
        List<SoilDataDTO> soilDataList = sensorDataService.getAllSoilData();
        return ResponseEntity.ok(soilDataList);
    }
    @GetMapping("/sub-table-data")
    public ResponseEntity<List<SubTableDataDTO>> getSubTableData(@RequestParam String tableName) {
        List<SubTableDataDTO> dataList = sensorDataService.getSubTableData(tableName);
        return ResponseEntity.ok(dataList);
    }
}
